<template>
  <div class="comments-container">
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户好评</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="comments-content">
        <div class="item" v-for="(item,i) of tableData" :key="i">
          <div class="flex-row align-center head">
            <template v-if="item.user">
              <img :src="item.user.headImg" alt="" class="cover"
                   v-if="item.user.headImg&&item.user.headImg.trim().length>0"/>
              <img src="../assets/img/img.jpg" alt="" class="cover" v-else/>
              <div class="p_x5 nickname">{{ item.user.name || '' }}</div>
            </template>

          </div>
          <div class="p_y20 p_x10 comment">
            {{ item.comment || '' }}
          </div>
          <div class="flex-row justify-between align-center font12 text-light foot">
            <div class="goods-info">
              <template v-if="item.car">
                【{{ item.car.carNumber || '' }}】
                {{ item.car.brand || '' }}.
                {{ item.car.model || '' }}.
                {{ item.car.colour || '' }}
              </template>
            </div>
            <div class="time">{{ format(item.updateTime) }}</div>
          </div>
        </div>
      </div>
      <div class="row-align-center footer">
        <div class="text-light font12" v-if="pageMeta.isEnd">---到底了---</div>
        <el-link class="" href="javascript:;" @click="nextPage" v-else>查看更多</el-link>
      </div>
    </el-card>

  </div>
</template>

<script>
import {computed, defineComponent, reactive, ref, toRaw} from "vue"
import {useRoute} from "vue-router";
import Goods from "../api/modules/Goods";
import {bcAdd, bcMul, deepClone} from "../utils/helpers";
import Toast from "../utils/Toast";
import Orders from "../api/modules/Orders";
import {useStore} from "vuex";
import {useUserLogin} from "../hooks/user/useUserLogin";
import Events from "../events/Events";
import dayjs from "dayjs";
import {usePageMeta} from "../hooks/usePageTableData";

export default defineComponent({
  name: "Comments",
  methods: {
    format(time, layout = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(time).format(layout)
    }
  },
  setup() {
    const pageHandle = usePageMeta();
    const params = reactive({
      filter: "",
      isComment: "yes"
    });
    const tableData = ref([]);
    onPageChange(1); // 初始化时直接请求一次分类列表
    async function onPageChange(page) {
      const pageMeta = deepClone(toRaw(pageHandle.pageMeta.value));
      if (page) {
        pageMeta.index = page;
      }
      try {
        const _params = {
          ...pageMeta,
          ...params,
        };
        const {data} = await Orders.lists(_params);
        const _data = data.data || [];
        if (_params.index > 1) {
          tableData.value.push(..._data);
        } else { // 第一页
          tableData.value = _data;
        }

        pageHandle.updatePageMeta(_params.index, data.count);

      } catch (e) {
        console.log(e);
      }
    }

    // 查看更多
    function nextPage() {
      if (pageHandle.pageMeta.value.isEnd) {
        Toast.warning('到底了');
        return
      }
      onPageChange(pageHandle.pageMeta.value.index + 1)
    }

    return {
      ...pageHandle,
      nextPage,
      tableData,
    }
  },
})
</script>

<style lang="scss" scoped>
.comments-content {
  padding: 20px 0;

  .item {
    margin-bottom: 10px;
    padding: 10px;
    border-bottom: solid 1px #f8f8f8;

    &:last-child {
      border-width: 0;
    }

    .head {
      .cover {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        border-width: 0;
      }
    }
  }
}

</style>
